// pages/classify/cate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    no_cate:'/static/images/empty/home_cate.png',
    isList: false, //是否以列表展示  | 列表或大图
    list: [],
    category: [],
    pageIndex:1,
    loadding:false,
    pullUpOn:false,
    allDone:false,
    title:'分类',
    fid:0,
    id:0
  },

  onLoad: function(e) {
    if(e.fid){
      this.setData({
        fid:e.fid,
        title:e.n
      })
      wx.setNavigationBarTitle({
        title:e.n
      })
    }
    if(e.id){
      this.setData({
        id:e.id
      })
    }
    this.cate();
    this.load();
  },
  async load(){
    if(this.data.allDone){
      return;
    }
    if(this.data.loadding){
      return;
    }
    let res=await wx.util.post("cate/itemList",{id:this.data.id,fid:this.data.fid,page:this.data.pageIndex},this.data.pageIndex>1);
    if(res.data.data.length>0){
      this.setData({
        pageIndex:this.data.pageIndex++
      })
    }
    if(res.data.data.length==0 || res.data.data.length<res.data.per_page){
      this.setData({
        allDone:true
      })
    }
    this.setData({
      loadding:false,
      pullUpOn:false,
      list:this.data.list.concat(res.data.data)
    })
  },
  async cate(){
    let res=await wx.util.post("cate/son",{id:this.data.fid});
    this.setData({
      category:res.data
    })
  },
  toCate(e){
    let id=e.currentTarget.dataset.id;
    this.setData({
      id:id,
      list:[],
      allDone:false,
      loadding:false,
      pullUpOn:false,
      pageIndex:1
    })
    this.load();
  },
  detail(id) {
      wx.navigateTo({
        url: '../productDetail/productDetail?id='+id
      });
  },
  onShareAppMessage(e) {
    let wxid=wx.getStorageSync("openid") || '';
      return {
        path: '/pages/classify/cate?wxid='+wxid+'&fid='+this.data.fid+'&n='+this.data.title,
      }
  },
  onShareTimeline(e) {
    let wxid=wx.getStorageSync("openid") || '';
      return {
      query: 'wxid='+wxid+'&fid='+this.data.fid+'&n='+this.data.title,
    }
  },
  onReachBottom() {
    this.load();
  }
})